Skip to content

bpo-18283: Add support for bytes to shutil.which#11818

Merged
vstinner merged 4 commits into
python:masterfrom
csabella:bpo18283
Feb 13, 2019
Merged

bpo-18283: Add support for bytes to shutil.which#11818
vstinner merged 4 commits into
python:masterfrom
csabella:bpo18283

Conversation

@csabella

@csabella csabella commented Feb 11, 2019

Copy link
Copy Markdown
Contributor

Original patch by Victor Stinner.

https://bugs.python.org/issue18283

Comment thread Lib/shutil.py
# Check that a given file can be accessed with the correct mode.
# Additionally check that `file` is not a directory, as on Windows
# directories pass the os.access check.
def _access_check(fn, mode):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious any reason over moving this outside which?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an author of the original patch, I can explain: it's inefficient to define a temporary function for each which() call. Moreover, there is no technical reason to use a nested function.

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM... well, I wrote the initial patch, so I'm not fair :-)

Just a few suggestions.

Comment thread Lib/shutil.py Outdated
Comment thread Lib/shutil.py Outdated
# PATHEXT is necessary to check on Windows.
pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
if isinstance(cmd, bytes):
pathext = list(map(os.fsencode, pathext))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use a list-comprehension instead? I'm not sure what is the best ;-)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that list(map(os.fsencode, pathext)) it's ok.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a timeit on both and the list(map()) is slightly faster (on my system), but not much. If it code after this didn't (potentially) iterate over pathext twice, a generator would be ideal.

Also, the following if clause uses:

    if any(cmd.lower().endswith(ext.lower()) for ext in pathext):
        files = [cmd]

Removing the cmd.lower() from the any would be a processing optimization, but it's unrelated to this change, so I didn't put it in.

    cmd_lower = cmd.lower()
    if any(cmd_lower.endswith(ext.lower()) for ext in pathext):
        files = [cmd]

Comment thread Lib/test/test_shutil.py Outdated

@eamanu eamanu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. it's work correctly

Comment thread Lib/shutil.py Outdated
# PATHEXT is necessary to check on Windows.
pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
if isinstance(cmd, bytes):
pathext = list(map(os.fsencode, pathext))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that list(map(os.fsencode, pathext)) it's ok.

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last remark :-)

Comment thread Lib/test/test_shutil.py Outdated
@vstinner

Copy link
Copy Markdown
Member

@giampaolo: I wrote the initial patch, so I would prefer that someone else would review it. Would mind to review this change please?

@giampaolo

Copy link
Copy Markdown
Contributor

@vstinner no problem; patch LGTM.

@vstinner
vstinner merged commit 5680f65 into python:master Feb 13, 2019
@vstinner

Copy link
Copy Markdown
Member

Thanks @giampaolo for the review ;-)

@vstinner

Copy link
Copy Markdown
Member

Thanks @csabella, I merged your PR ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants